package cn.edu.sjtu.message_management.controller;

import cn.edu.sjtu.message_management.model.Message;
import cn.edu.sjtu.message_management.model.RespStatus;
import cn.edu.sjtu.message_management.model.Result;
import cn.edu.sjtu.message_management.model.User;
import cn.edu.sjtu.message_management.service.MessageService;
import cn.edu.sjtu.message_management.util.JWTUtil;
import cn.edu.sjtu.message_management.util.ResultUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
public class MessageController {
    @Autowired
    private WebSocketController webSocketController;
    @Autowired
    private MessageService messageService;

    //服务器捕获用户发送的消息，将其存入数据库后通知用户
    @RequestMapping(value = "/message", method = RequestMethod.POST)
    public Result listenMessage(@Valid @RequestBody Message message, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(RespStatus.WRONG_INFORMATION);
        }
        //存储message
        messageService.save(message);
        List<User> userList = message.getToUser();
        try {
            //推送通知
            webSocketController.notice(userList);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultUtil.error(RespStatus.SERVER_ERROR);
        }
        return ResultUtil.success(null);
    }

    //获取消息列表（包括已发、未读、已读消息列表）
    @RequestMapping(value = "/message", method = RequestMethod.GET)
    public Result getMessage(@RequestHeader("Authorization") String token) {
        String userId = JWTUtil.decodeUserId(token);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("read", messageService.getReadMessage(userId));
        jsonObject.put("unread", messageService.getUnReadMessage(userId));
        jsonObject.put("sent", messageService.getSentMessage(userId));
        return ResultUtil.success(jsonObject);
    }

    //获取已发消息列表
    @RequestMapping(value = "/message/sent", method = RequestMethod.GET)
    public Result getSentMessage(@RequestHeader("Authorization") String token) {
        String userId = JWTUtil.decodeUserId(token);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sent", messageService.getSentMessage(userId));
        return ResultUtil.success(jsonObject);
    }

    //获取已读消息列表
    @RequestMapping(value = "/message/read", method = RequestMethod.GET)
    public Result getReadMessage(@RequestHeader("Authorization") String token) {
        String userId = JWTUtil.decodeUserId(token);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("read", messageService.getReadMessage(userId));
        return ResultUtil.success(jsonObject);
    }

    //获取未读消息列表
    @RequestMapping(value = "/message/unread", method = RequestMethod.GET)
    public Result getUnreadMessage(@RequestHeader("Authorization") String token) {
        String userId = JWTUtil.decodeUserId(token);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("unread", messageService.getUnReadMessage(userId));
        return ResultUtil.success(jsonObject);
    }

    //设置消息为已读
    @RequestMapping(value = "/message/{messageId}", method = RequestMethod.PUT)
    public Result readMessage(@PathVariable("messageId") String messageId) {
        if (messageService.read(messageId)) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(RespStatus.SERVER_ERROR);
    }

    //设置消息一键已读
    @RequestMapping(value = "/allMessage", method = RequestMethod.PUT)
    public Result readAllMessage(@RequestHeader("Authorization") String token) {
        String userId = JWTUtil.decodeUserId(token);
        if (messageService.readAll(userId)) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(RespStatus.SERVER_ERROR);
    }
}
